package com.taotao.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.taotao.utils.FastDFSClient;

@Controller
public class PictureController {
	@Value("${IMAGE_SERVER_URL}")
	private String image_server_url;
	
	@RequestMapping(value="/pic/upload")
	@ResponseBody
	public Map<String, Object> picUpload(MultipartFile uploadFile ){
		try {
			//得到后缀名
			String originalFilename = uploadFile.getOriginalFilename();
			String extName = originalFilename.substring(originalFilename.lastIndexOf(".")+1);
			//上传文件
			FastDFSClient fastDFSClient = new FastDFSClient("classpath:properties/client.conf");
			String uploadFile2 = fastDFSClient.uploadFile(uploadFile.getBytes(), extName);
			
			String url = image_server_url+uploadFile2;
			
			Map<String, Object> map = new HashMap<>();
			map.put("error", 0);
			map.put("url", url);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			Map<String, Object> map = new HashMap<>();
			map.put("error", 1);
			map.put("message", "图片上传失败");
			return map;
		}
		
		
	}
}
